package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.OfficialNewsDAO;
import com.wanmait.hotelManage.vo.OfficialNews;
import com.wanmait.hotelManage.vo.Pager;

@WebServlet("/manage/OfficialNewsServlet")
public class OfficialNewsServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if("list".equals(action)) {
			this.list(request, response);
		}else if("delete".equals(action)) {
			this.delete(request, response);
		}else if("batchDelete".equals(action)) {
			this.batchDelete(request, response);
		}else if("insert".equals(action)) {
			this.insert(request, response);
		}else if("getOfficialNewss".equals(action)) {
			this.getOfficialNewss(request, response);
		}else if("search".equals(action)) {
			this.search(request, response);
		}else if("getSearchOfficialNewss".equals(action)) {
			this.getSearchOfficialNewss(request, response);
		}
	}
	//搜索完分页
	public void getSearchOfficialNewss(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pageNo = request.getParameter("pageNo");
		String keyword = request.getParameter("keyword");
		Pager pager=new Pager();
		pager.setPageNo(Integer.parseInt(pageNo));
		pager.setPageSize(8);
		int rows=OfficialNewsDAO.getOfficialnewsdao().getRows(keyword);
		pager.setPageCount(rows);
	
		List<OfficialNews> searchOfficialNewss = OfficialNewsDAO.getOfficialnewsdao().findOfficialNewssBykeyword(keyword, pager);
		request.setAttribute("searchOfficialNewss", searchOfficialNewss);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/officialNews/showSearchOfficialNews.jsp").forward(request, response);
	}
	//搜索
	public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String keyword = request.getParameter("keyword");
		Pager pager=new Pager();
		pager.setPageSize(8);
		pager.setPageNo(1);
		int rows=OfficialNewsDAO.getOfficialnewsdao().getRows(keyword);
		pager.setPageCount(rows);
		List<OfficialNews> officialNewss=OfficialNewsDAO.getOfficialnewsdao().findOfficialNewssBykeyword(keyword,pager);
		request.setAttribute("pager", pager);
		request.setAttribute("officialNewss", officialNewss);
		request.setAttribute("keyword", keyword);
		request.getRequestDispatcher("/manage/officialNews/searchOfficialNews.jsp").forward(request, response);
	}
	//分页
	public void getOfficialNewss(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageSize(8);
		pager.setPageNo(Integer.parseInt(request.getParameter("pageNo")));
		int rows=OfficialNewsDAO.getOfficialnewsdao().getRows();
		pager.setPageCount(rows);
		List<OfficialNews> allOfficialNews = OfficialNewsDAO.getOfficialnewsdao().findAllOfficialNews(pager);
		request.setAttribute("pager", pager);
		request.setAttribute("allOfficialNews", allOfficialNews);
		request.getRequestDispatcher("/manage/officialNews/showOfficialNews.jsp").forward(request, response);
	}
	//发送消息
	public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		OfficialNews officialNews=new OfficialNews();
		officialNews.setTitle(title);
		officialNews.setContent(content);
		OfficialNewsDAO.getOfficialnewsdao().insert(officialNews);
		response.sendRedirect(request.getContextPath()+"/manage/OfficialNewsServlet?action=list");
	}
	//批量删除
	public void batchDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ids = request.getParameter("ids");
		OfficialNewsDAO.getOfficialnewsdao().delete(ids);
	}
	//根据id单条删除
	public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		OfficialNewsDAO.getOfficialnewsdao().delete(Integer.parseInt(id));
		this.getOfficialNewss(request, response);
	}
	//展示全部
	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageSize(8);
		pager.setPageNo(1);
		int rows=OfficialNewsDAO.getOfficialnewsdao().getRows();
		pager.setPageCount(rows);
		List<OfficialNews> officialNewss=OfficialNewsDAO.getOfficialnewsdao().findAllOfficialNews(pager);
		request.setAttribute("officialNewss", officialNewss);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/officialNews/list.jsp").forward(request, response);
	}

}
